<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>upload2</title>
    <link rel="stylesheet" href="../lib/bootstrap.css">
    <script src="../lib/jquery.js"></script>
</head>

<body>
    <input type="file" id="file1">
    <button>上传</button>
    <img src="./image/loading.gif" alt="" style="display: none;" id="loading" />
    <script>
        $(function () {
            $(document).ajaxStart(function(){
                $("#loading").show();
            })
            $(document).ajaxStop(function(){
                $("#loading").hide();
            })
            $("button").on("click", function () {
                var files = $("#file1")[0].files
                if (files.length <= 0) return alert("please upload files")


                var fd = new FormData();
                fd.append('avatar', files[0])

                $.ajax({
                    method: "POST",
                    url: 'http://www.liulongbin.top:3006/api/upload/avatar',
                    data: fd,
                    processData: false,
                    contentType: false,
                    success: (res) => {
                        console.log(res);
                    }
                })
            })
        })
    </script>
</body>

</html>